package com.hankol.feature_wine_meal.adapter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.hankol.feature_wine_meal.bean.RecommendFoodBean
import com.hankol.feature_wine_meal.fragment.EliteFragment
import java.util.ArrayList

/**
 * @author: 关忆北、
 * @date: 2022/6/21
 * @Des：
 */
class EliteVpFragmentAdapter(var fm: FragmentManager) : FragmentPagerAdapter(fm) {

    private var beans: ArrayList<RecommendFoodBean> = arrayListOf()
    private var fragments: ArrayList<Fragment> = arrayListOf()
    fun setData(beans: ArrayList<RecommendFoodBean>) {
        if (fragments.isNotEmpty()) {
            val beginTransaction = fm.beginTransaction()
            for (f in this.fragments) {
                beginTransaction.remove(f)
            }
            beginTransaction.commit()
            fm.executePendingTransactions()
        }
        this.beans.clear()
        this.fragments.clear()
        this.beans.addAll(beans)

        notifyDataSetChanged()
    }

    override fun getCount(): Int {
        return beans.size
    }

    override fun getItem(position: Int): Fragment {
        val mFragment = EliteFragment.newInstance(beans[position].appetizerDietList, beans[position].id, beans[position].name)
        fragments.add(mFragment)
        return mFragment
    }

    override fun getItemPosition(`object`: Any): Int {
        return POSITION_NONE
    }
}